package org.esk.dablog.model.entries;

import org.esk.dablog.model.Entry;
import org.esk.dablog.model.Author;
import org.esk.dablog.model.Comment;
import org.esk.dablog.model.comments.RideJoinComment;

import javax.persistence.*;
import java.util.Date;
import java.util.Set;
import java.util.Collections;

/**
 * This class is used to process Rides
 * User: jc
 * Date: 22.11.2006
 * Time: 10:49:50
 * $Id:$
 *
 * @hibernate.subclass discriminator-value="1"
 */
@Entity
@DiscriminatorValue("1")
public class Ride extends Entry {
    public static final int STATE_NEW = 0;
    public static final int STATE_CANCELLED = 1;

    private Set rideJoinComments;

    /**
     * checks in ride is (not) cancelled
     * @return
     */
    @Transient
    public boolean isCancelled() {
        return getState() == STATE_CANCELLED;
    }

    /**
     * Allows user to join ride
     *
     * @param author
     */
    public RideJoinComment join(Author author) {
        RideJoinComment rideJoin = new RideJoinComment();
        rideJoin.setDate(new Date());
        rideJoin.setAuthor(author);
        this.addComment(rideJoin);
        return rideJoin;
    }

    /**
     * splits author from the ride
     *
     * @param author
     */
    public void split(Author author) {
        Comment toRemove = null;
        for (Comment c : getRideJoinComments()) {
            if (c.getAuthor().getUsername().equals(author.getUsername())) {
                toRemove = c;
                break;
            }
        }
        if (toRemove != null) {
            getComments().remove(toRemove);
        }
    }

    /**
     * returns how many people has joined the ride
     *
     * @return
     */
    @Transient
    public int getJoined() {
        return getRideJoinComments() == null ? 0 : getRideJoinComments().size();
    }

    /**
     * Checks if author is on the ride
     *
     * @param username
     * @return
     */
    @Transient
    public boolean isOnRide(String username) {
        if (username == null || rideJoinComments == null) {
            return false;
        }
        for (Comment c : getRideJoinComments()) {
            if (c.getAuthor() != null && username.equals(c.getAuthor().getUsername())) {
                //TODO handle "already joined" case
                return true;
            }
        }
        return false;
    }

    /**
     * @return
     * @hibernate.set lazy="true"
     * inverse="true"
     * cascade="none"
     * order-by="ID"
     * where="comment_type=1"
     * @hibernate.one-to-many class="org.esk.dablog.model.comments.RideJoinComment"
     * @hibernate.key column="ENTRY_ID"
     */
    @OneToMany
    @JoinColumn(name="ENTRY_ID", insertable=false, updatable=false)
    @OrderBy("id")
    public Set<RideJoinComment> getRideJoinComments() {
        return rideJoinComments;
    }

    public void setRideJoinComments(Set rideJoinComments) {
        this.rideJoinComments = Collections.unmodifiableSet(
                rideJoinComments != null ? rideJoinComments : Collections.EMPTY_SET);
    }

}
